package com.summit.exam.servlet;

import com.summit.exam.dbdao.UserInfoDao;
import com.summit.exam.entity.UserInfoBean;
import com.summit.exam.utils.MyDialog;
import sun.rmi.runtime.Log;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import javax.xml.ws.Response;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class RegitsterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取账号密码
        String username = req.getParameter("ipt_name");
        String password = req.getParameter("ipt_pass");

        UserInfoDao userInfoDao = new UserInfoDao();
        UserInfoBean userInfoBean = userInfoDao.loginSystem(username, password);

        if (userInfoBean != null){
            if(userInfoBean.getUserLoginName() != null && userInfoBean.getUserLoginPwd() != null){
                req.getSession().setAttribute("ipt_name", username);
                req.getSession().setMaxInactiveInterval(600);
                req.getSession().setAttribute("username",userInfoBean.getUserLoginName());
                if (userInfoBean.getUserLevel().equals("0")){

                    resp.sendRedirect( "/SummitExam/ExamSystem/techweb/adminIndex.jsp");
                }else {
                    resp.sendRedirect("/SummitExam/ExamSystem/techweb/examineIndex.jsp");
                }

            }else {
                req.setAttribute("message", "用户名或密码错误");
                req.getRequestDispatcher("/ExamSystem/techweb/register.jsp").forward(req, resp);
            }
        }else {
            req.setAttribute("message", "用户不存在");
            req.getRequestDispatcher("/ExamSystem/techweb/register.jsp").forward(req, resp);
        }
    }

}
